package com.zz.apk.utils

import android.os.SystemClock
import com.zz.framework.core.ext.printError
import com.zz.framework.core.ext.toDateString
import com.zz.framework.core.utils.XPreference


/**
 * Copyright © 2022 zhun All rights reserved.
 * Created by Zha0Zhun on 2022/10/8 12:08.
 * @author: Zha0Zhun 1820022519@qq.com
 * @version: V1.0
 */
object RealTimeUtil {
    private var serverTime by XPreference("SP_SERVER_TIME", 0L)
    private var elapsedRealtime by XPreference("SP_ELAPSED_REALTIME", SystemClock.elapsedRealtime())

    fun updateServerTime(time:Long){
        if (time==0L)return;
        if (time<10000000000){
            serverTime = time.times(1000)
        }else {
            serverTime = time
        }
        elapsedRealtime = SystemClock.elapsedRealtime()

        getRealTime().toDateString().printError()
    }
    fun getRealTime():Long{
        if (serverTime ==0L){
            return System.currentTimeMillis()
        }else {
            return serverTime + SystemClock.elapsedRealtime() - elapsedRealtime
        }
    }
}